﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using PivotData;
using PivotIO.XML;

namespace PivotIO.Tests
{
    [TestClass]
    public class XMLIOTests
    {
        [TestInitialize]
        public void TestReset()
        {
            Utils.TestInit();
        }

        [TestCleanup]
        public void TestFinalize()
        {
            Utils.TestFinalize();
        }

        [TestMethod]
        public void TestLoadEmpty()
        {
            var cl = CollectionInfoIO.LoadCollection(1);
            Assert.IsNull(cl, "Collection snould be null");
        }

        [TestMethod]
        public void TestLoadCollectionByNameEmpty()
        {
            var cr = CollectionInfoIO.LoadCollection("hithere");
            Assert.IsNull(cr, "collection");
        }

        [TestMethod]
        public void TestLoadEmptyFullList()
        {
            var cl = CollectionInfoIO.ListAllCollections();
            Assert.AreEqual(0, cl.Length, "# of collections");
        }

        [TestMethod]
        public void TestLoadAndSaveOneCollection()
        {
            var ci = new CollectionInfo() { Name = "hithere", Id = 0 };
            CollectionInfoIO.SaveCollection(ci);
            var cr = CollectionInfoIO.LoadCollection(0);
            Assert.IsNotNull(cr, "collection");
            Assert.AreEqual("hithere", cr.Name, "Name of collection");
        }

        [TestMethod]
        public void TestLoadCollectionByName()
        {
            var ci = new CollectionInfo() { Name = "hithere", Id = 0 };
            CollectionInfoIO.SaveCollection(ci);
            var cr = CollectionInfoIO.LoadCollection("hithere");
            Assert.IsNotNull(cr, "collection");
            Assert.AreEqual("hithere", cr.Name, "Name of collection");
        }

        [TestMethod]
        public void TestListWithACollection()
        {
            var ci = new CollectionInfo() { Name = "hithere", Id = 0 };
            CollectionInfoIO.SaveCollection(ci);
            Assert.AreEqual(1, CollectionInfoIO.ListAllCollections().Length, "# of collections");
        }

        [TestMethod]
        public void TestCollectionIdMaxEmpty()
        {
            var id = CollectionInfoIO.GetMaxCollectionId();
            Assert.AreEqual(0, id, "collection max id");
        }

        [TestMethod]
        public void TestDeleteCollection()
        {
            var ci = new CollectionInfo() { Name = "hi there", Id = 0 };
            CollectionInfoIO.SaveCollection(ci);
            CollectionInfoIO.DeleteCollection(ci);
            Assert.AreEqual(0, CollectionInfoIO.ListAllCollections().Length, "# of collections");
        }

        [TestMethod]
        public void TestCollectionIdMax()
        {
            CollectionInfoIO.SaveCollection(new CollectionInfo() { Name = "hi there", Id = 25 });
            CollectionInfoIO.SaveCollection(new CollectionInfo() { Name = "hi there dude", Id = 17 });
            var id = CollectionInfoIO.GetMaxCollectionId();
            Assert.AreEqual(25, id, "collection max id");
        }

        [TestMethod]
        public void TestNoMarkerFileLeftBehind()
        {
            // When we create a collection there should be no marker file.
            var ci = new CollectionInfo() { Name = "hithere", Id = 10 };
            CollectionInfoIO.SaveCollection(ci);

            Assert.IsFalse(Utils.RenderPending(10), "A render was pending, unexpected");
        }
    }
}
